package com.woniu.car.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.domain.TRolePermission;
import com.woniu.car.domain.TRoleUser;
import com.woniu.car.domain.TUrl;
import com.woniu.car.dto.PermissionsDto;
import com.woniu.car.exception.AccountException;
import com.woniu.car.exception.ParamException;
import com.woniu.car.mapper.TRolePermissionMapper;
import com.woniu.car.param.BackstageQueryPermissionParam;
import com.woniu.car.param.UrlRolesJson;
import com.woniu.car.service.TRolePermissionService;
import com.woniu.car.service.TRoleUserService;
import com.woniu.car.service.TUrlService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Service
public class TRolePermissionServiceImpl extends ServiceImpl<TRolePermissionMapper, TRolePermission> implements TRolePermissionService {
    @Resource
    private TRoleUserService tRoleUserService;
    @Resource
    private TUrlService tUrlService;
    /**
     *@Author 王维鑫
     *@Description 新增角色权限
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public void insertRolePermission(TRolePermission tRolePermission) throws Exception {
        boolean save = this.save(tRolePermission);
        if(save==false){
            throw new Exception("新增权限失败");
        }
    }
    /**
     *@Author 王维鑫
     *@Description 修改角色权限
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public void updateRolePermission(TRolePermission tRolePermission) throws Exception {
        boolean b = this.updateById(tRolePermission);
        if(b==false){
            throw new Exception("修改权限失败");
        }
    }
    /**
     *@Author 王维鑫
     *@Description 查看角色权限列表
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TRolePermission> showRolePermission() {
        List<TRolePermission> list = this.list();
        return list;
    }
    /**
     *@Author 王维鑫
     *@Description 查询角色权限
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TRolePermission> queryRolePermission(TRolePermission tRolePermission) {
        QueryWrapper<TRolePermission> wrapper = new QueryWrapper<>();
        if(null!=tRolePermission.getRoleId()){
            wrapper.eq("role_id",tRolePermission.getRoleId());
        }
        if(null!=tRolePermission.getPermissionId()){
            wrapper.eq("permission_id",tRolePermission.getPermissionId());
        }
        List<TRolePermission> list = this.list(wrapper);
        return list;
    }
    /**
     *@Author 王维鑫
     *@Description 根据员工ID查询权限
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<PermissionsDto> queryPermissionById(BackstageQueryPermissionParam param) throws ParamException {
        ArrayList<TUrl> names = new ArrayList<>();
        //根据ID查到用户所有角色
        TRoleUser tRoleUser = new TRoleUser();
        tRoleUser.setUserId(param.getBackstageId());
        List<TRoleUser> tRoleUsers = tRoleUserService.queryRoleUser(tRoleUser);
        if(tRoleUsers.size()==0){
            throw new AccountException("该用户未分配角色");
        }else {
            //用户对应的角色ID
            ArrayList<Integer> roleIds = new ArrayList<Integer>();
            for (TRoleUser roleUser : tRoleUsers) {
                Integer roleId = roleUser.getRoleId();
                roleIds.add(roleId);
            }
            //查看所有接口对应的角色
            List<TUrl> tUrls = tUrlService.showAllUrls();
            if(tUrls.size()==0){
                throw new AccountException("暂未查到功能信息");
            }else {
                //存储对象中的角色
                ArrayList<Integer> urls = new ArrayList<Integer>();
                for (TUrl url : tUrls) {
                    //判断每个对象中的角色跟用户对应角色有没有相同
                    String roleIds1 = url.getRoleIds();
                    if(null!=roleIds1){
                        List<UrlRolesJson> gameList = JSONObject.parseArray(roleIds1, UrlRolesJson.class);
                        if(gameList!=null){
                            for (UrlRolesJson urlRolesJson : gameList) {
                                Integer roleId = urlRolesJson.getRoleId();
                                for (Integer id : roleIds) {
                                    if(id.equals(roleId)){
                                        //将当前url对象保存
                                        names.add(url);
                                        System.out.println("对象："+url);
                                        continue;
                                    }
                                }
                            }
                        }
                    }

                }
            }
        }
        if(names.size()==0){
            throw new ParamException("当前用户没有权限");
        }
        List<PermissionsDto> permissionsDtos = BeanCopyUtils.copyList(names, PermissionsDto::new);
        return permissionsDtos;
    }


    //
}
